package cn.dujc.superkid.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import cn.dujc.core.ui.IBaseUI;
import cn.dujc.core.util.GsonUtil;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.activities.LoginActivity;
import cn.dujc.superkid.bean.ChannelEntity;
import cn.dujc.superkid.bean.RegisterResult;

/**
 * @author du
 * date 2018/8/1 下午8:58
 */
public class Preferences {

    public static final int GO_LOGIN_REQUEST_CODE = 9123;
    private static final String NAME = "AppPreferences";

    private static final Preferences sInstance = new Preferences();

    public static Preferences getInstance() {
        return sInstance;
    }

    private SharedPreferences mPreferences;
    private RegisterResult mRegisterResult;

    private Preferences() { }

    private SharedPreferences getPreferences(Context context) {
        if (mPreferences == null) {
            synchronized (Preferences.class) {
                if (mPreferences == null) {
                    mPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
                }

            }
        }
        return mPreferences;
    }

    public void saveLoginResult(Context context, RegisterResult loginResult) {
        getPreferences(context).edit()
                .putString("login", GsonUtil.toJsonString(mRegisterResult = loginResult))
                .apply();
    }

    public RegisterResult getLoginResult(Context context) {
        if (mRegisterResult == null) {
            final String login = getPreferences(context)
                    .getString("login", "");
            return mRegisterResult = GsonUtil.fromJson(login, RegisterResult.class);
        } else {
            return mRegisterResult;
        }
    }

    public void saveChannel(Context context, ChannelEntity channelEntity) {
        getPreferences(context).edit()
                .putString("channel", GsonUtil.toJsonString(channelEntity))
                .apply();
    }

    public ChannelEntity getChannel(Context context) {
        final String channel = getPreferences(context)
                .getString("channel", "");
        return GsonUtil.fromJson(channel, ChannelEntity.class);
    }

    /**
     * 是否登录
     *
     * @return 0 为已登录，大于0的值为未登录
     */
    public static boolean isLogin(Activity activity) {
        return isLogin(new IBaseUI.IContextCompatActivityImpl(activity));
    }

    /**
     * 是否登录
     *
     * @return 0 为已登录，大于0的值为未登录
     */
    public static boolean isLogin(Fragment fragment) {
        return isLogin(new IBaseUI.IContextCompatFragmentImpl(fragment));
    }

    /**
     * 是否登录
     *
     * @return 0 为已登录，大于0的值为未登录
     */
    public static boolean isLogin(IBaseUI.IContextCompat context) {
        final String login = getInstance().getPreferences(context.context())
                .getString("login", "");
        if (TextUtils.isEmpty(login)) {
//            goLogin(context);
            return false;
        }
        final RegisterResult fromJson = GsonUtil.fromJson(login, RegisterResult.class);
        if (fromJson == null) {
//            goLogin(context);
            return false;
        }
        return true;
    }

    private static void goLogin(IBaseUI.IContextCompat context) {
        ToastUtil.showToast(context.context(), "您还未登录，请先登录");
        Intent intent = new Intent(context.context(), LoginActivity.class);
        context.startActivityForResult(intent, GO_LOGIN_REQUEST_CODE);
    }

    public static boolean loginSuccess(int requestCode, int resultCode){
        return resultCode == Activity.RESULT_OK && requestCode == Preferences.GO_LOGIN_REQUEST_CODE;
    }
}
